using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DiscuzRobot
{
    public class DiscuzDataWaterArmy
    {
        private static DiscuzDataWaterArmy instance;

        public static DiscuzDataWaterArmy Instance
        {
            get {
                if (instance == null)
                    instance = new DiscuzDataWaterArmy();
                return instance; }
        }
        public DiscuzDataWaterArmy()
        {
            _List_Data = new List<WaterArmyData>();
            Initial(Global.APP_DefautlWaterArmyPath);
        }
        private List<WaterArmyData> _List_Data;

        public List<WaterArmyData> List_Data
        {
            get { return _List_Data; }
            set { _List_Data = value; }
        }
        public void Initial(string path)
        {
            if (!File.Exists(path))
                return;

            _List_Data.Clear();
            string[] ary = File.ReadAllLines(path, Encoding.Default);
            foreach (string s in ary)
            {
                string[] ary1 = Regex.Split(s, @"\s+");

                _List_Data.Add(new WaterArmyData(ary1[0], ary1[1]));
            }
        }
    }

    public class WaterArmyData
    {
        public string uname, upwd;
        public Robot robot;
        public WaterArmyData(string uname, string upwd)
        {
            this.uname = uname;
            this.upwd = upwd;
            this.robot = new Robot();
        }
        public WaterArmyData(SiteInfo si)
        {
            this.uname = si.UserName;
            this.upwd = si.UserPwd;
            this.robot = new Robot();
        }
        public static WaterArmyData GetSiteUser()
        {
            return new WaterArmyData(SiteInfo.Instance);
        }
    }

    public class DiscuzDataPosted
    {
        public DiscuzDataPosted()
        {
            _List_Data = new List<PostedInfo>();
            Initial(Global.APP_DefautlPostedPath);
        }
        private List<PostedInfo> _List_Data;

        public List<PostedInfo> List_Data
        {
            get { return _List_Data; }
            set { _List_Data = value; }
        }
        public void Initial(string path)
        {
            if (!System.IO.Directory.Exists(path))
                return;
            string[] file = System.IO.Directory.GetFiles(path);
            _List_Data.Clear();
            foreach (string s in file)
            {
                _List_Data.Add(new PostedInfo(s, s.Replace(path + "\\", "")));
            }
        }
        public void Delete(PostedInfo pi)
        {
            _List_Data.Remove(pi);
        }

        public PostedInfo AddNewpd()
        {
            string path = Global.APP_DefautlPostedPath;
            if (!System.IO.Directory.Exists(path))
                return null;
            PostedInfo pi = null;
            string[] file = System.IO.Directory.GetFiles(path);
            foreach (string s in file)
            {
                pi = new PostedInfo(s, s.Replace(path + "\\", ""));
                bool exists = List_Data.Exists((Predicate<PostedInfo>)delegate(PostedInfo s1)
                      {
                          if (s1.path == s)
                              return true;
                          else
                              return false;
                      }
                      );

                if (!exists)
                {
                    _List_Data.Add(pi);
                    return pi;
                }
            }
            return null;
        }
    }
    public class PostedInfo
    {
        public string subject, message, path, filename;
        public List<string> Huitie;
        public PostedInfo(List<string> Huitie)
        {
            this.Huitie = Huitie;

        }
        public PostedInfo(string path,string filename)
        {
            this.path = path;
            this.filename = filename;
            this.subject = filename.Replace(".txt", "");
            this.message = File.ReadAllText(path);
        }
    }

    public class DiscuzDataHuitie
    {
        public DiscuzDataHuitie()
        {
            _List_Data = new List<string>();
            Initial(Global.APP_DefautlHuitiePath);
        }
        private List<string> _List_Data;

        public List<string> List_Data
        {
            get { return _List_Data; }
            set { _List_Data = value; }
        }
        public void Initial(string path)
        {
            if (!File.Exists(path))
                return;

            _List_Data.Clear();
            string[] ary = File.ReadAllLines(path, Encoding.Default);
            foreach (string s in ary)
            {
                _List_Data.Add(s);
            }
        }
    }

}
